class Solution {
public:
    int myAtoi(string s) {
        if(s.empty()) return 0;
        //if(s.size() >= 11) return INT_MAX;
        long num = 0;
        int flag = 1; 
        int index = 0;
        while(s.size() > 0 && s[index] == ' ') index++;  //去除前导空格
        if(((s[index] == '-') && (s[index + 1] == '+')) ) return 0;
        if(s[index] == '-') flag *= -1, index++;  //如果是负数,flag标记为负数
        if(s[index] == '+') index++;

        //跳过非数字字符
        while(s[index] < '0' && s[index] > '9') index++;
        
        //处理数字字符
        while(s[index] >= '0' && s[index] <= '9'){
           if(num - 1 > INT_MAX) break;
           num = num * 10 + s[index] - '0'; 
           index++;
        }
        num *= flag;
        if(num > INT_MAX) num = INT_MAX;
        if(num < INT_MIN) num = INT_MIN;
        return num;  
    }
};